package bank.service.spring.login;

import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import bank.model.properties.DBGroupsTable;
import bank.model.properties.DBLoginTable;
import bank.model.properties.DBTables;
import bank.model.sql.builder.SqlString;

public class LoginController implements Controller, DBTables, DBLoginTable, DBGroupsTable {
	
	public LoginController() {
		super();
	}	
	
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultSet result = null;
		int id = 0;
		String str_id = "";
		SqlString mes = new SqlString();
		String name = request.getParameter("name");
		String pass = request.getParameter("pass");
		
		mes.set_Select();
		mes.set_Table_Name(TBL_LOGIN);
		mes.add_Param(LOGIN_COL_GROUP_ID, null);
		mes.add_Cond(LOGIN_COL_LOGIN,name);
		mes.add_Cond(LOGIN_COL_PASSWORD, pass);
		result = mes.executeQuery();
		if(!result.first())
			return new ModelAndView("index");
		
			System.out.print(id = result.getInt(LOGIN_COL_GROUP_ID));
		
		mes.clear_All();
		mes.set_Select();
		mes.set_Table_Name(TBL_GROUPS);
		mes.add_Param(GROUPS_COL_NAME, null);
		mes.add_Cond(GROUPS_COL_ID, String.valueOf(id));
		result = mes.executeQuery();
		if(!result.first())		
			return new ModelAndView("index");
		System.out.print(str_id = result.getString(GROUPS_COL_NAME));
		
		
		
		if(str_id.equals("boss"))	
			return new ModelAndView("login/Boss", "enter", true);
		if(str_id.equals("admin"))		
			return new ModelAndView("login/Admin", "enter", true);
		if(str_id.equals("employee"))		
			return new ModelAndView("login/Employee", "enter", true);
		
		return new ModelAndView("index");
	}
	
}